
package com.flashextended.apps.logomaker.controlers
{
	import com.flashextended.apps.logomaker.beans.workareaobjects.GenericObject;
	import com.flashextended.apps.logomaker.beans.workareaobjects.ImageOnStage;
	import com.flashextended.apps.logomaker.beans.workareaobjects.TextOnStage;
	import com.flashextended.apps.logomaker.io.IOMediator;
	import com.flashextended.apps.logomaker.model.Model;
	import com.flashextended.apps.logomaker.singleton.AppControl;
	import com.flashextended.apps.logomaker.singleton.AppLocales;
	import com.flashextended.apps.logomaker.view.windows.AddSymbolW;
	import com.flashextended.apps.logomaker.view.windows.previewlogo.PreviewLogoW;
	import com.flashextended.utils.graphics.windows.AlertInfoWindow;
	
	import flash.display.Bitmap;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	/**
	 * Handles controls through the application
	 */ 
	public class Controlers
	{
		private static var controlers:Controlers;
		public function Controlers()
		{
		}
		
		public static function getInstance():Controlers
		{
			if(controlers==null)
			{
				controlers = new Controlers();
			}
			return controlers;
		}
		
		/**
		 * add listener for buttons
		 * @listener - the function that will respond to Mouse click
		 */ 
		public function addMouseAction(c:UIComponent, listener:Function):void
		{
			c.addEventListener(MouseEvent.CLICK, listener);
		}
		
		/**
		 * click handler for the info button
		 */ 
		public function infoListener(e:MouseEvent):void
		{
			var infoAlert:AlertInfoWindow = new AlertInfoWindow(350, 220, null, AppLocales.getInstance().data.locales['infoWindow']);
			infoAlert.slectableTxt = true;
			infoAlert.removeCancelButton();
			PopUpManager.addPopUp(infoAlert, AppControl.getInstance().logoMaker.parent);
			PopUpManager.centerPopUp(infoAlert);
			infoAlert.infoText = AppLocales.getInstance().data.locales['applicationInfo'];
			infoAlert.show();
		}
		/**
		 * mouse handler for the new project button
		 */ 
		public function newProjectListener(e:MouseEvent):void
		{
			var infoAlert:AlertInfoWindow = new AlertInfoWindow(300, 160, newProjectHandler, AppLocales.getInstance().data.locales['alert']);
			PopUpManager.addPopUp(infoAlert, AppControl.getInstance().logoMaker.parent);
			PopUpManager.centerPopUp(infoAlert);
			infoAlert.infoText = AppLocales.getInstance().data.locales["infoNewProject"];
			infoAlert.show();
		}
		
		private function newProjectHandler(val:Boolean):void
		{
			(val)?AppControl.getInstance().logoMaker.startApp():null;
		}
		
		/**
		 * mouse handler for the add text button
		 */ 
		public function addTextListener(e:MouseEvent):void
		{
			AppControl.getInstance().workarea.addObject(new TextOnStage(AppLocales.getInstance().data.locales['initialTextToTextObject']));
		}
		
		/**
		 * mouse handler for the add image button
		 */ 
		public function addImageListener(e:MouseEvent):void
		{
			IOMediator.getInstance().broker.uploadImage(getImage);
		}
		private function getImage(img:Bitmap):void
		{
			AppControl.getInstance().workarea.addObject(new ImageOnStage(img));
		}
		
		/**
		 * mouse handler for the add symbol button
		 */ 
		public function addSymbolListener(e:MouseEvent):void
		{
			var displaySymbols:AddSymbolW = new AddSymbolW(630, 400, AppLocales.getInstance().data.locales["addSymbol"]);
			PopUpManager.addPopUp(displaySymbols, AppControl.getInstance().workarea);
			PopUpManager.centerPopUp(displaySymbols);
			displaySymbols.show();
		}		
		
		/**
		 * mouse handler for the save logo button
		 */ 
		public function saveLogoListener(e:MouseEvent):void
		{
			AppControl.getInstance().workarea.unselectObjects();
			IOMediator.getInstance().broker.saveLogo();
		}
		
		/**
		 * mouse handler for the save project button
		 * @see Model
		 */ 
		public function saveProjectListener(e:MouseEvent):void
		{
			AppControl.getInstance().workarea.unselectObjects();
			Model.getInstance().saveProject();
		}
		
		/**
		 * mouse handler for the load project button
		 * @see Model
		 */ 
		public function loadProjectListener(e:MouseEvent):void
		{
			AppControl.getInstance().logoMaker.startApp();
			Model.getInstance().loadProject();		
		}
		
		/**
		 * alert - curent project will be lost
		 */ 
		private function loadProjectHandler(val:Boolean):void
		{
			if(val)
			{
				AppControl.getInstance().logoMaker.startApp();
				Model.getInstance().loadProject();	
			}
		}		
		
		
		/**
		 * mouse handler for the preview logo button
		 */ 
		public function previewLogoListener(e:MouseEvent):void
		{
			AppControl.getInstance().workarea.unselectObjects();
			var bmp:Bitmap = null;
			try{
			bmp = AppControl.getInstance().workarea.getLogo();
			}catch(e:Error){};
			var previewLogo:PreviewLogoW = new PreviewLogoW(630, 400, bmp, AppLocales.getInstance().data.locales["previewLogo"]);
			PopUpManager.addPopUp(previewLogo, AppControl.getInstance().workarea);
			PopUpManager.centerPopUp(previewLogo);
			previewLogo.show();
		}
		
		/**
		 * mouse handler for the object selection from the workarea
		 */ 
		public function selectObjectWorkarea(e:MouseEvent):void
		{
			AppControl.getInstance().workarea.selectObject(GenericObject(e.currentTarget));
		}												
		
		/**
		 * mouse handler for the object deselection from the workarea
		 */ 
		public function unselectObjectsWorkarea(e:MouseEvent):void
		{
			AppControl.getInstance().workarea.unselectObjects();
		}
		
		/*
		Begin controls for objects properties
		/////////////////////////////////////
		*/
		public function moveObjectUp(e:MouseEvent):void
		{
			AppControl.getInstance().workarea.moveObjectUp();
		}
		public function moveObjectDown(e:MouseEvent):void
		{
			AppControl.getInstance().workarea.moveObjectDown();
		}
		public function removeObject(e:MouseEvent):void
		{
			var infoAlert:AlertInfoWindow = new AlertInfoWindow(300, 160, removeObjectHandler, AppLocales.getInstance().data.locales['alert']);
			PopUpManager.addPopUp(infoAlert, AppControl.getInstance().logoMaker.parent);
			PopUpManager.centerPopUp(infoAlert);
			AppControl.getInstance().logoMaker.enabled = false;
			infoAlert.infoText = AppLocales.getInstance().data.locales["infoDelete"];
			infoAlert.show();
		}
		private function removeObjectHandler(val:Boolean):void
		{
			AppControl.getInstance().logoMaker.enabled = true;
			(val)?AppControl.getInstance().workarea.removeObject():null;
		}

	}
}